#include "mainscene.h"
#include "config.h"
#include "QIcon"
#include "QPainter"
#include <QMouseEvent>
#include "ctime"
#include "QSound"

MainScene::MainScene(QWidget *parent) : QWidget(parent)
{
    // 初始化场景
    initScene();
    // 开始游戏
    playGame();
}

MainScene::~MainScene()
{

}

void MainScene::initScene()
{
    // 设置宽高
    setFixedSize(GAME_WIDTH, GAME_HEIGHT);
    // 设置标题
    setWindowTitle(GAME_TITLE);
    // 加载图标
    setWindowIcon(QIcon(":/res/app.ico"));
    // 定时器设置
    m_timer.setInterval(GAME_RATE);
    // 敌机出场时间
    m_record = 0;
    // 随机数种子
    srand((unsigned int)time(NULL));
}

void MainScene::playGame()
{
    QSound::play(SOUND_BACKGROUND);
    m_timer.start();
    connect(&m_timer, &QTimer::timeout, [=](){
        // 敌机出场
        enemyToScene();
        // 更新游戏位置
        updatePosition();
        // 绘制窗口
        update();
        // 碰撞检测
        collisionDetection();
    });
}

void MainScene::updatePosition()
{
    // 更新地图坐标
    m_map.mapPosition();
    // 发射子弹
    m_plane.shoot();
    // 设置非空闲子弹的当前坐标
    for (int i = 0;i < BULLET_NUM; i++)
    {
        // 如果非空闲，计算发射位置
        if (!m_plane.m_bullets[i].m_free)
        {
            m_plane.m_bullets[i].updatePosition();
        }
    }
    // 敌机出场
    for (int i = 0;i < ENEMY_NUM; i++)
    {
        if (!m_enemys[i].m_free)
        {
            m_enemys[i].updatePosition();
        }
    }
    // 爆炸图片
    for (int i = 0;i < BOMB_NUM;i++)
    {
        if (!m_bombs[i].m_free)
        {
            m_bombs[i].updateInfo();
        }
    }
}

void MainScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    // 绘制地图
    painter.drawPixmap(0, m_map.m_map1_posy, m_map.m_map1);
    painter.drawPixmap(0, m_map.m_map2_posy, m_map.m_map2);
    // 绘制飞机
    painter.drawPixmap(m_plane.m_x, m_plane.m_y, m_plane.m_plane);
    // 绘制子弹
    for (int i = 0;i < BULLET_NUM; i++)
    {
        if (!m_plane.m_bullets[i].m_free)
        {
            int x = m_plane.m_bullets[i].m_x;
            int y = m_plane.m_bullets[i].m_y;
            painter.drawPixmap(x, y, m_plane.m_bullets[i].m_bullet);
        }
    }
    // 绘制敌机
    for (int i = 0;i < ENEMY_NUM; i++)
    {
        if (!m_enemys[i].m_free)
        {
            int x = m_enemys[i].m_x;
            int y = m_enemys[i].m_y;
            painter.drawPixmap(x, y, m_enemys[i].m_enemy);
        }
    }
    // 绘制爆炸特效
    for (int i = 0;i < BOMB_NUM; i++)
    {
        if (!m_bombs[i].m_free)
        {
            int x = m_bombs[i].m_x;
            int y = m_bombs[i].m_y;
            painter.drawPixmap(x, y, m_bombs[i].m_pixarr[m_bombs[i].m_index]);
        }
    }
}

void MainScene::mouseMoveEvent(QMouseEvent * event)
{
    int x = event->x() - m_plane.m_rect.width() / 2;
    int y = event->y() - m_plane.m_rect.height() / 2;
    if (x <= 0)
    {
        x = 0;
    }
    else if (x >= GAME_WIDTH - m_plane.m_rect.width())
    {
        x = GAME_WIDTH - m_plane.m_rect.width();
    }
    if (y <= 0)
    {
        y = 0;
    }
    else if (y >= GAME_HEIGHT - m_plane.m_rect.height())
    {
        y = GAME_HEIGHT - m_plane.m_rect.height();
    }
    m_plane.setPosition(x, y);
}

void MainScene::enemyToScene()
{
    m_record++;
    if (m_record < ENEMY_INTERVAL)
    {
        return;
    }
    m_record = 0;
    for (int i =0; i < ENEMY_NUM; i++)
    {
        if (m_enemys[i].m_free)
        {
            m_enemys[i].m_free = false;
            // 坐标
            m_enemys[i].m_x = rand() % (GAME_WIDTH - m_enemys[i].m_rect.width());
            m_enemys[i].m_y = - m_enemys[i].m_rect.height();
            break;
        }
    }
}

void MainScene::collisionDetection()
{
    for (int i = 0; i < ENEMY_NUM; i++)
    {
        if (m_enemys[i].m_free)
        {
            continue;
        }
        for (int j = 0; j < BULLET_NUM; j++)
        {
            if (m_plane.m_bullets[j].m_free)
            {
                continue;
            }
            if (m_enemys[i].m_rect.intersects(m_plane.m_bullets[j].m_rect))
            {
                m_enemys[i].m_free = true;
                m_plane.m_bullets[j].m_free = true;
                // 播放爆炸特效
                for(int k = 0;k < BOMB_NUM;k++)
                {
                    if (m_bombs[k].m_free)
                    {
                        QSound::play(SOUND_BOMB);
                        m_bombs[k].m_free = false;
                        m_bombs[k].m_x = m_enemys[i].m_x;
                        m_bombs[k].m_y = m_enemys[i].m_y;
                        break;
                    }
                }
            }
        }
    }
}
